import 'dart:math';

import 'package:asperm/pages/start_testing_page.dart';
import 'package:asperm/tools/const.dart';
import 'package:asperm/views/custom_cell.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:image_picker/image_picker.dart';

import '../tools/configuration.dart';
import '../tools/motility.dart';
import '../tools/motility_analysis_result.dart';
import '../tools/tools.dart';
import '../views/myDialog.dart';

@immutable
class ReportPage extends StatefulWidget {
  final MotilityAnalysisResult result;

  ReportPage({
    super.key,
    required this.result,
  });


  @override
  State<ReportPage> createState() => _ReportPageState(result: this.result);
}

class _ReportPageState extends State<ReportPage> {
  MotilityAnalysisResult result;

  _ReportPageState({required this.result});
  String dateString = '';
  int score = 0;
  String scoreDesc = '';
  String motilityText = '';
  String concentrationText = '';
  String PMSCText = '';




  @override
  void initState() {
    super.initState();

    processingData();
  }



  processingData(){
    dateString = getDateStringFromTimestamp(result.createdTime);
    score = getScore();
    scoreDesc = getScoreDesc();
    motilityText = getMotilityText();
    concentrationText = getConcentrationText();
    PMSCText = getPMSCText();

    print('=====分析完成！');
    String string_1 = '检测到的精子数量: ${result.numOfDetectedSperms}\n';
    String string_2 = 'A级精子数量: ${result.numOfALevelSperms}\n';
    String string_3 = 'B级精子数量: ${result.numOfBLevelSperms}\n';
    String string_4 ='C级精子数量: ${result.numOfCLevelSperms}\n';
    String string_5 ='活动轨迹数量: ${result.numOfMotileTrajectories}\n';
    String string_6 ='平均总位移: ${result.avgTotalDisplacement.toStringAsFixed(2)}\n';
    String string_7 ='平均VSL: ${result.avgVsl.toStringAsFixed(2)}\n';
    String string_8 ='平均VCL: ${result.avgVcl.toStringAsFixed(2)}\n';
    String string_9 ='平均VAP: ${result.avgVap.toStringAsFixed(2)}\n';
    String string_10 ='平均ALH: ${result.avgAlh.toStringAsFixed(2)}\n';

    String result_string = string_1 + string_2 + string_3 + string_4 + string_5 + string_6 + string_7 + string_8 + string_9 + string_10;
    print('=====分析结果：${result_string}');
    setState(() {});

  }

  getScore(){
    var motility = getMotility();
    var concentration = getConcentration();
    var PMSC = motility * concentration;

    if (PMSC <= 4.35) {
      return 10;
    } else if (PMSC <= 5.58) {
      return 20;
    } else if (PMSC <= 7.92 ) {
      return 30;
    } else if (PMSC <= 16.2) {
      return 40;
    } else if (PMSC <= 36.3) {
      return 50;
    } else if (PMSC <= 69.3) {
      return 60;
    } else if (PMSC <= 117.9) {
      return 70;
    } else if (PMSC <= 160.2) {
      return 80;
    } else {
      return 90;
    }
  }
  getMotilityText(){
    return  "Progressive motility: ${(getMotility() * 100).toStringAsFixed(2)} (%)";
  }

  getMotility(){
    if (result.numOfDetectedSperms > 0) {
      return (result.numOfALevelSperms + result.numOfBLevelSperms).toDouble() / (result.numOfDetectedSperms).toDouble();
    }
    return 0;
  }
  getConcentrationText()  {
    return  "Sperm concentration: ${getConcentration().toStringAsFixed(2)} (M/mL)";
  }

  getConcentration() {
    return (result.numOfDetectedSperms).toDouble() / 2.351;
  }

  getScoreDesc(){
    if (score <= 10) {
      return 'Don’t worry. Sperm motility is a bit weak. Consult a doctor for better chances of conceiving.';
    } else if (score <= 20) {
      return 'The sperm condition has a little challenge. Adjust your diet and sleep.';
    } else if (score <= 60) {
      return 'Sperm motility is all right. Cut down on smoking and drinking for better pregnancy prep.';
    } else {
      return 'Wow! Your sperm has strong motility. Start the pregnancy journey relaxed.';
    }
  }

  getPMSCText() {
    return "PMSC: ${getPMSC().toStringAsFixed(2)} (M/mL)";
  }

  getPMSC(){
    var motility = getMotility();
    var concentration = getConcentration();
    var PMSC = motility * concentration;
    return PMSC;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Color(0xffF9F9F9),
        appBar: AppBar(
          title: Text(
            'Asperm Score',
            style: TextStyle(fontSize: 18.sp,fontWeight: FontWeight.w600,color: Color(0xff1B1B1B),),
          ),
        ),
        body: Container(
          height: screenHeight(context),
          child: Container(
            width: double.infinity,
            color: Color(0xffF9F9F9),
            child: contentWidget(context),
          ),
        )
    );
  }

  contentWidget(BuildContext context) {
    return Container(
      padding: EdgeInsets.only(left: 20.w,right: 20.w),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Text(
            dateString,
            style: TextStyle(fontSize: 16.sp,fontWeight: FontWeight.w500,color: Color(0xff1B1B1B),),
          ),
          SizedBox(height: 20.h,),
          Text(
            '${score}',
            style: TextStyle(fontSize: 20.sp,fontWeight: FontWeight.w600,color: Colors.green,),
          ),
          SizedBox(height: 20.h,),
          Container(
            child: Text(
              scoreDesc,
              style: TextStyle(fontSize: 14.sp,fontWeight: FontWeight.w500,color: Color(0xff1B1B1B),),
            ),
          ),
          SizedBox(height: 20.h,),
         Container(
           width: double.infinity,
           child: Column(
             mainAxisAlignment: MainAxisAlignment.start,
             crossAxisAlignment: CrossAxisAlignment.start,
             children: [
               Text(
                 concentrationText,
                 style: TextStyle(fontSize: 14.sp,fontWeight: FontWeight.w500,color: Color(0xff1B1B1B),),
               ),
               SizedBox(height: 20.h,),
               Text(
                 motilityText,
                 style: TextStyle(fontSize: 14.sp,fontWeight: FontWeight.w500,color: Color(0xff1B1B1B),),
               ),
               SizedBox(height: 20.h,),
               Text(
                 PMSCText,
                 style: TextStyle(fontSize: 14.sp,fontWeight: FontWeight.w500,color: Color(0xff1B1B1B),),
               ),
             ],
           ),
         ),
          Container(
            height: 48.h,
            width: 335.w,
            decoration: BoxDecoration(
              color: Color.fromRGBO(16, 37, 105, 1),
              borderRadius: BorderRadius.circular(5.h),
            ),
            margin: EdgeInsets.only(left: 0.w, right: 0.w, top: 60.h),
            child: TextButton(
              onPressed: () {

                // Navigator.pushNamedAndRemoveUntil(context, '/', (route) => false);
                Navigator.pop(context);
                Navigator.pop(context);
                Navigator.pop(context);
              },
              child: Text(
                'BACK',
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 16.sp,
                  fontWeight: FontWeight.w400,

                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  centerMenuWidget(BuildContext context){
    return Column(
      children: [

      ],
    );
  }






}

